<?php
	session_start();
	set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
	$PEAR_PATH = 'PEAR.php';
	include ('Request/Request.php');
	include "functions.php";
	$lang = getLang();

	global $viewstate;
	global $postback;
	global $cookie;

	//get page of selected cache
	$url = "http://www.geocaching.com/seek/cache_details.aspx?guid=".$_GET['guid'];
	$req = & new HTTP_Request($url);
	$req->setMethod(HTTP_REQUEST_METHOD_GET);
	$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
	$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
	$req->sendRequest();
	$response = $req->getResponseBody();

	//error if cache is archived or disabled
	$error = '';
	if(strpos($response, '<span id="ctl00_ContentBody_ErrorText"', $position)) {
		if (strpos($response, 'archived', $position)) {
			$error = "archived!";
		} elseif (strpos($response, 'unavailable', $position)) {
			$error = "disabled!";
		}
	}

	if(!empty($error)) { //error output
		$html = '<?xml version="1.0" encoding="UTF-8"?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html>
	  			<head>
	    			<title>Error</title>
	  			</head>
	  			<body class="alert">
	    			<p>Cache '.$error.'</p>
	  			</body>
			</html>';
		echo $html;
	} else {	//output if no error
		//parsing additional waypoints table
		$position = 0;
		$response = parseBetween('<strong>Additional Waypoints</strong>', '</table>');
	  	$table = array();
		$i = 0;
		$rightAW = false;
		$aw = array();
		while($position = strpos(' '.$response, '<tr')) {
			$allTable = $response;
			$position = 0;
			$tr = parseBetween('<tr bgcolor', '</tr>');
			$tr = substr($tr, (strpos($tr, '>')+1));
			$table[$i] = array();
			while($position = strpos(' '.$tr, '<td')) {
				$response = $tr;
				$position = 0;
				$td = parseBetween('<td', '</td>');
				$td = substr($td, (strpos($td, '>')+1));

				$table[$i][] = $td;
				$tr = substr($tr, (strpos($tr, '</td>')+5));
			}
			
			if($rightAW) {
				$aw['note'] = $table[$i];
				break;
			}
			if($table[$i][3] == $_GET['lookup']) {
				$aw['data'] = $table[$i];
				$rightAW = true;
			}

			$response = substr($allTable, (strpos($allTable, '<tr bgcolor')+11));
			$response = substr($response, (strpos($response, '</tr>')+5));
			$i++;
		}
		
		if($aw['data'][5] != '???') {
			$coords = explode(' ', $aw['data'][5]);

			$lat_h = $coords[0];
			$lat_d = substr($coords[1], 0, 2);
			$lat_m = $coords[2];
		
			$lon_h = $coords[3];
			$lon_d = substr($coords[4], 0, 3);
			$lon_m = $coords[5];
		
			//converting coordinates to requested format
			$lat = latDMtoD($lat_h, $lat_d, $lat_m);
			$lon = lonDMtoD($lon_h, $lon_d, $lon_m);

			$kml = '<?xml version="1.0" encoding="UTF-8"?>
				<kml xmlns="http://earth.google.com/kml/2.2">
		  			<Placemark>
		    			<name><![CDATA['.$aw['data'][3].']]></name>
		    			<description><![CDATA['.$aw['note'][1].']]></description>
		    			<Point>
		      			<coordinates>'.$lon.','.$lat.'</coordinates>
		    			</Point>
		  			</Placemark>
				</kml>';
			echo $kml;
		} else { //output if AW haven't coordinates
			$position = 0;
			$cacheName = parseBetween('<span id="ctl00_ContentBody_CacheName">', '</span>');
			$html = '<?xml version="1.0" encoding="UTF-8"?>
				<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
				<html>
		  			<head>
		    			<title><![CDATA['.$cacheName.']]></title>
		  			</head>
		  			<body class="form">
			  		<form action="'.SERVICE_URL.'cache.php?guid='.$_GET['guid'].'" method="post">
		  				<fieldset>
				  			<p>'.$aw['data'][3].'</p>
							<p>'.$aw['note'][1].'</p>
				  			<input type="submit" value="'.$lang['back'].'" />
		  				</fieldset>
			  		</form>
	  				</body>
				</html>';
			echo $html;
		}
	}
?>
